package io.binghe.concurrent.lab06;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

/**
 *  创建一个 ForkJoinPool 线程池，支持任务并行执行，并且使用work-stealing）算法来提高 CPU 利用率
 * @author CurleyG
 * @date 2025/3/25 23:58
 */


public class WorkStealingPoolExample {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newWorkStealingPool();

        // 提交多个任务
        IntStream.range(1, 10).forEach(i ->
                executor.submit(() -> {
                    System.out.println(Thread.currentThread().getName() + " 执行任务 " + i);
                    try {
                        Thread.sleep(1000); // 模拟任务执行
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                })
        );

        // 等待子任务执行完
        Thread.sleep(3000);
    }
}
